package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.data.Page;
import com.dongdongshop.mapper.TbBrandMapper;
import com.dongdongshop.pojo.brand.TbBrand;
import com.dongdongshop.service.Brand.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Service
@Component
public class BrandServiceImpl implements BrandService {

    @Resource
    private TbBrandMapper brandMapper;

    /**
     * 分页查询所有的品牌信息，通过自定义的工具类返回，省去了web层添加分页依赖
     */
    @Override
    public Page<TbBrand> getBrandInfo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbBrand> tbBrands = brandMapper.selectByExample(null);
        PageInfo<TbBrand> pageInfo = new PageInfo<>(tbBrands);

        Page<TbBrand> page = new Page();
        page.setTotal(pageInfo.getTotal());
        page.setRows(pageInfo.getList());
        return page;
    }

    /*
    * 增加品牌数据
    * */

    @Override
    public void addBrand(TbBrand b) {
        brandMapper.insertSelective(b);
    }

    /*
    * 根据品牌id查询品牌信息，回显
    * */
    @Override
    public TbBrand getBrandById(Long id) {
        TbBrand tbBrand = brandMapper.selectByPrimaryKey(id);
        return tbBrand;
    }

    /*
    * 根据品牌id修改品牌信息
    * */
    @Override
    public void updateBrandById(TbBrand b) {
        brandMapper.updateByPrimaryKeySelective(b);
    }

    /**
     * 根据品牌id删除品牌信息
     */
    @Override
    public void deleteBrandById(Long id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    /*
    * 不分页查询所有品牌信息
    * */
    @Override
    public List<TbBrand> findBrandsAll() {
        return brandMapper.selectByExample(null);
    }
}
